package com.yfproject.service.impl;

import java.util.List;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date; 
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.yfproject.business.LabelsRepository;
import com.yfproject.models.Labels;
import com.yfproject.models.custommodels.LevelCount;
import com.yfproject.models.custommodels.LevelCount2;
import com.yfproject.service.LableService;

@Transactional(rollbackOn=Exception.class)
@Service
public class LabelServiseImpl implements LableService {
	
	@Autowired
	LabelsRepository labelsrepository;
	
	/**
	 * 重载findByNameAndStatus,如果参数有Pageable,则调用此方法
	 */
	@Override
	public Page<Labels> findByNameAndStatus(String name,Integer status, Pageable pageable) {
		
		return labelsrepository.findByNameAndStatus(name, status, pageable);
	}
	
	/**
	 * 重载qryParentLabel,如果参数有Pageable,则调用此方法
	 */
	@Override
	public Page<Labels> qryParentLabel(Long parentId, Pageable pageable) {
		
		return labelsrepository.findByParentId(parentId,pageable);
	}

	/**
	 * 重载queryByParentIdAndStatus,如果参数有Pageable,则调用此方法
	 */
	@Override
	public Page<Labels> queryByParentIdAndStatus(Long parentId, int status, Pageable pageable) {
		
		return labelsrepository.findByParentIdAndStatus(parentId,status,pageable);
	}

	/**
	 * 重载findByName,如果参数有Pageable,则调用此方法
	 */
	@Override
	public Page<Labels> findByName(String name, Pageable pageable) {
		
		return labelsrepository.findByName(name, pageable);
	}
	
	public List<Labels> qryLabel(Long id) {
		return labelsrepository.findById(id);
	}
	
	public List<Labels> qryParentLabel(Long id) {
		
		return labelsrepository.findByParentId(id);
	}	
	
	public List<Labels> queryByParentIdAndStatus(Long id, int status) {
		
		return labelsrepository.findByParentIdAndStatus(id,status);
	}	

	public LevelCount qryLevel(int level){
		List<Labels> labelsList = labelsrepository.findByLevel(level);
		LevelCount levelcount = new LevelCount();
		levelcount.setCount(labelsList.size());
		levelcount.setLabelsList(labelsList);
		
		return levelcount;
	}
	
	public List<LevelCount2> qryAndCountByLevel(int level){
		List<Labels> labelsList = labelsrepository.findByLevel(level);
		List<LevelCount2> listLevelCount2  = new ArrayList<LevelCount2>();
		for(int i = 0; i < labelsList.size();i++) {
			LevelCount2 levelCount2 = new LevelCount2();
			levelCount2.setCountSubByLevel(qryParentLabel(labelsList.get(i).getId()).size());
			levelCount2.setName(labelsList.get(i).getName());
			levelCount2.setId(labelsList.get(i).getId());
			levelCount2.setLevel(labelsList.get(i).getLevel());
			levelCount2.setStatus(labelsList.get(i).getStatus());
			levelCount2.setParentId(labelsList.get(i).getParentId());
			levelCount2.setLast_update_time(labelsList.get(i).getLast_update_time());
			levelCount2.setDescription(labelsList.get(i).getDescription());
			levelCount2.setEx_1(labelsList.get(i).getEx_1());
			levelCount2.setEx_2(labelsList.get(i).getEx_2());
			levelCount2.setEx_3(labelsList.get(i).getEx_3());
			listLevelCount2.add(levelCount2);
		}
		return listLevelCount2;
	
	}
	
	@CacheEvict(cacheNames="lebel",allEntries=true)
	public void delLabel(Long id) {
		List<Labels> labelsList = labelsrepository.findById(id);
		
		Labels labels = labelsList.get(0);          //id为主键，表中唯一值，所以查询出来list长度为1
		labels.setStatus(9);                        //设置状态为9 停止使用
		Date date = new Date();       
		Timestamp nowTime = new Timestamp(date.getTime());
		labels.setLast_update_time(nowTime);
		labelsrepository.save(labels);

		//int level = labels.getLevel();
		if(!labelsrepository.findByParentId(id).isEmpty()){        //判断这个标签是否还有子标签(是最后一级标签) 
			List<Labels> subLabelList = labelsrepository.findByParentId(id);  //根据id作为父节点来查找子节点
			int subSize = subLabelList.size();
			int indexSubLabel = 0;
			for(;indexSubLabel < subSize;indexSubLabel++){                    //循环遍历以该id为父级标签的子标签
				delLabel(subLabelList.get(indexSubLabel).getId());
			}
		}
		return;
	}
	
	//没有description
	public Void insertLabel(String name, int level, int status, Long parent_id) {
		Labels labels = new Labels();
		labels.setName(name);
		labels.setLevel(level);
		//在添加时设置状态
		//1 准备使用
		//2 正在使用
		//9 停止使用
		labels.setStatus(status);
		labels.setParentId(parent_id);
		labelsrepository.save(labels);
		return null;
	}	

	public Void insertLabel(String name, int level, int status, Long parent_id, String description) {
		Labels labels = new Labels();
		labels.setName(name);
		labels.setLevel(level);
		//在添加时设置状态
		//1 准备使用
		//2 正在使用
		//9 停止使用
		labels.setStatus(status);
		labels.setParentId(parent_id);
		labels.setDescription(description);
		labelsrepository.save(labels);
		return null;
	}
	//没有description
	public Void modifyLabel(Long id, String name) {
		//根据ID修改name
		List<Labels>labelsList=labelsrepository.findById(id);
		Labels labels=labelsList.get(0);
		labels.setId(id);
		labels.setName(name);
		Date date = new Date();       
		Timestamp nowTime = new Timestamp(date.getTime());
		labels.setLast_update_time(nowTime);
		labelsrepository.save(labels);
		return null;
	}
	public Void modifyLabel(Long id, String name, String description) {
		//根据ID修改name,description
		List<Labels>labelsList=labelsrepository.findById(id);
		Labels labels=labelsList.get(0);
		labels.setId(id);
		labels.setName(name);
		labels.setDescription(description);
		Date date = new Date();       
		Timestamp nowTime = new Timestamp(date.getTime());
		labels.setLast_update_time(nowTime);
		labelsrepository.save(labels);
		return null;
	}

	public List<Labels> findByName(String name) {
		return labelsrepository.findByName(name);
	}
	
	public List<Labels> findByNameAndStatus(String name,Integer status){
		return labelsrepository.findByNameAndStatus(name, status);
	}


}
